package com.cetcs.kmga.dataManager.service.impl.dataManager;

import com.alibaba.fastjson.JSON;
import com.cetc.cloud.kmga.util.DateUtils;
import com.cetcs.kmga.common.Data;
import com.cetcs.kmga.dataManager.dao.mybatis.ClientDBMapper;
import com.cetcs.kmga.dataManager.dao.mybatis.FTPFlowStatisticMapper;
import com.cetcs.kmga.dataManager.dao.mybatis.LogQueryMapper;
import com.cetcs.kmga.dataManager.entity.dataManager.*;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FTPFlowListVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FTPFlowPointVo;
import com.cetcs.kmga.dataManager.entity.dataManager.vo.FTPFlowTotalVo;
import com.cetcs.kmga.dataManager.entity.device.vo.KeyAndValueVo;
import com.cetcs.kmga.dataManager.global.FlowStatImageUrlEnum;
import com.cetcs.kmga.dataManager.global.ResLogType;
import com.cetcs.kmga.dataManager.service.dataManager.FTPFlowStatisticService;
import com.cetcs.kmga.dataManager.util.Pinyin4jUtil;
import com.cetcs.kmga.dataManager.util.RequestUtil;
import com.google.common.collect.Lists;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * FTP流量统计服务层
 *
 * @author xutao
 * @version V1.0 创建时间：2017-11-09 11:07
 *          Copyright 2017 by CETC
 */
@Service
public class FTPFlowStatisticServiceImpl implements FTPFlowStatisticService {

    private final static Logger LOGGER = LoggerFactory.getLogger(FTPFlowStatisticServiceImpl.class);

    @Autowired
    private FTPFlowStatisticMapper ftpFlowStatisticMapper;

    @Autowired
    private LogQueryMapper logQueryMapper;


    /**
     * @param ftpId    采集数据源的ID
     * @param startDay 起始时间
     * @param endDay   结束时间
     * @return
     */
    @Override
    public List<FTPFlowListVo> findFTPDayFlowList(String ftpId, String startDay, String endDay) {
        String beginDate, endDate;//起始 与终止 用于向数据库传输
        int dayBw = 30;
        if (StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay)) {
            beginDate = startDay;
            endDate = endDay;
            dayBw = DateUtils.daysBetween(DateUtils.parse(beginDate, DateUtils.DATE_FORMAT_1), DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1));
            //如果结束时间不为空起始时间为空默认取终止时间7天前的日期
        } else if (!StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay)) {
            endDate = endDay;
            beginDate = DateUtils.format(DateUtils.getDateBefore(DateUtils.parse(endDay, DateUtils.DATE_FORMAT_1), 30), DateUtils.DATE_FORMAT_1);
            //如果终止时间为空，起始时间不为空
        } else if (StringUtils.isNotBlank(startDay) && !StringUtils.isNotBlank(endDay)) {
            beginDate = startDay;
            int num = DateUtils.daysBetween(DateUtils.parse(beginDate, DateUtils.DATE_FORMAT_1), new Date( ));
            if (num > 30) {
                endDate = DateUtils.format(DateUtils.getDateBefore(DateUtils.parse(startDay, DateUtils.DATE_FORMAT_1), -30), DateUtils.DATE_FORMAT_1);
            } else {
                dayBw = num;
                endDate = DateUtils.format(new Date( ), DateUtils.DATE_FORMAT_1);
            }
        } else {
            beginDate = DateUtils.format(DateUtils.getDateBefore(new Date( ), 31), DateUtils.DATE_FORMAT_1);
            endDate = DateUtils.format(DateUtils.getDateBefore(new Date( ), 1), DateUtils.DATE_FORMAT_1);
        }
        //采集端采集量
        List<FTPFlowStatPojo> dbCollectList = ftpFlowStatisticMapper.findDayCollectFlowListByFtpId(ftpId, beginDate, endDate);
        //资源采集量
        List<FTPFlowStatPojo> dbResrcList = ftpFlowStatisticMapper.findDayResrcFlowListByFtpId(ftpId, beginDate, endDate);
        List<FTPFlowListVo> ret = Lists.newArrayList( );
        Date endD = DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1);
        for (int i = 0; i < dayBw; i++) {
            FTPFlowListVo singleVo = new FTPFlowListVo( );
            singleVo.setInNums(Lists.newArrayList( ));
            singleVo.setOutNums(Lists.newArrayList( ));
            String statTime = DateUtils.format(DateUtils.getDateBefore(endD, i), DateUtils.DATE_FORMAT_1);
            singleVo.setStatTime(statTime);
            List<FTPFlowStatPojo> singleCollectList = dbCollectList.stream( ).filter(p -> statTime.equals(p.getStatTime( ))).map(p -> {
                String type = ResLogType.FTPCJRZ.toString( );
                p.setType(type);
                return p;
            }).collect(Collectors.toList( ));
            if (singleCollectList != null && singleCollectList.size( ) > 0) {
                //求和求总数
                long total = singleCollectList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
                FTPFlowStatPojo fsp = new FTPFlowStatPojo( );
                fsp.setType("TYPE");
                fsp.setNum(String.valueOf(total));
                fsp.setName("SUM");
                singleCollectList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
                singleCollectList.add(fsp);
                singleVo.setInNums(singleCollectList);
            }
            List<FTPFlowStatPojo> singleResrcList = dbResrcList.stream( ).filter(p -> statTime.equals(p.getStatTime( ))).map(p -> {
                ResLogType resLogType = ResLogType.getResLogType(p.getType( ));
                p.setType(resLogType.toString( ));
                return p;
            }).collect(Collectors.toList( ));
            if (singleResrcList != null && singleResrcList.size( ) > 0) {
                long total = singleResrcList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
                FTPFlowStatPojo fsp = new FTPFlowStatPojo( );
                fsp.setType("TYPE");
                fsp.setNum(String.valueOf(total));
                fsp.setName("SUM");
                singleResrcList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
                singleResrcList.add(fsp);
                singleVo.setOutNums(singleResrcList);
            }
            if (singleResrcList.size( ) == 0 && singleCollectList.size( ) == 0) {
                continue;
            } else {
                ret.add(singleVo);
            }
        }
        return ret;
    }

    /**
     * @param ftpId   采集数据源ID
     * @param timeDay 某天日期
     * @return
     */
    @Override
    public List<FTPFlowListVo> findFTPHourFlowList(String ftpId, String timeDay) {
        String currentDayStr = DateUtils.format(new Date( ), DateUtils.DATE_FORMAT_1);
        if (!StringUtils.isNotBlank(timeDay)) {
            timeDay = currentDayStr;
        }
        timeDay = timeDay.trim( );
        //采集端采集量
        List<FTPFlowStatPojo> dbCollectList = ftpFlowStatisticMapper.findHourCollectFlowListByFtpId(ftpId, timeDay);
        //资源采集量
        List<FTPFlowStatPojo> dbResrcList = ftpFlowStatisticMapper.findHourResrcFlowListByFtpId(ftpId, timeDay);
        int hourNum = 24;
        if (timeDay.equals(currentDayStr)) {
            hourNum = DateUtils.getHour(new Date( ));
        }
        List<FTPFlowListVo> ret = Lists.newArrayList( );
        for (int i = hourNum; i > 0; i--) {
            FTPFlowListVo singleVo = new FTPFlowListVo( );
            singleVo.setInNums(Lists.newArrayList( ));
            singleVo.setOutNums(Lists.newArrayList( ));
            String starTime = getTimeStr(i - 1);
            String endTime = getTimeStr(i);
            String dbTime = timeDay + " " + starTime;
            List<FTPFlowStatPojo> singleCollectList = dbCollectList.stream( ).filter(p -> dbTime.equals(p.getStatTime( ))).map(p -> {
                String type = ResLogType.FTPCJRZ.toString( );
                p.setType(type);
                return p;
            }).collect(Collectors.toList( ));
            if (singleCollectList != null && singleCollectList.size( ) > 0) {
                //求和求总数
                long total = singleCollectList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
                FTPFlowStatPojo fsp = new FTPFlowStatPojo( );
                fsp.setType("TYPE");
                fsp.setNum(String.valueOf(total));
                fsp.setName("SUM");
                singleCollectList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
                singleCollectList.add(fsp);
                singleVo.setInNums(singleCollectList);
            }
            List<FTPFlowStatPojo> singleResrcList = dbResrcList.stream( ).filter(p -> dbTime.equals(p.getStatTime( ))).map(p -> {
                ResLogType resLogType = ResLogType.getResLogType(p.getType( ));
                p.setType(resLogType.toString( ));
                return p;
            }).collect(Collectors.toList( ));
            if (singleResrcList != null && singleResrcList.size( ) > 0) {
                long total = singleResrcList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
                FTPFlowStatPojo fsp = new FTPFlowStatPojo( );
                fsp.setType("TYPE");
                fsp.setNum(String.valueOf(total));
                fsp.setName("SUM");
                singleResrcList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
                singleResrcList.add(fsp);
                singleVo.setOutNums(singleResrcList);
            }
            String statTime = starTime + ":00" + "-" + endTime + ":00";
            singleVo.setStatTime(statTime);
            if (singleResrcList.size( ) == 0 && singleCollectList.size( ) == 0) {
                continue;
            } else {
                ret.add(singleVo);
            }
        }
        return ret;
    }

    /**
     * 组装时间格式
     *
     * @param num
     * @return
     */
    private String getTimeStr(int num) {
        if (num > 24) {
            num = 24;
        }
        String timeStr = "00";
        if (num < 10) {
            timeStr = "0" + num;
        } else {
            timeStr = String.valueOf(num);
        }
        return timeStr;
    }

    /**
     * @param ftpId   采集数据源ID
     * @param timeDay 统计日期
     * @return
     */
    @Override
    public FTPFlowTotalVo findDayFlowTotalNum(String ftpId, String name, String timeDay) {
        String currentDayStr = DateUtils.format(new Date( ), DateUtils.DATE_FORMAT_1);
        if (!StringUtils.isNotBlank(timeDay)) {
            timeDay = currentDayStr;
        }
        timeDay = timeDay.trim( );
        FTPFlowTotalVo ftpFlv = new FTPFlowTotalVo( );
        ftpFlv.setCollectNums(Lists.newArrayList( ));
        ftpFlv.setDataBaseNums(Lists.newArrayList( ));
        long inTotal = 0;
        //采集端采集量
        List<FTPFlowStatPojo> dbCollectList = ftpFlowStatisticMapper.findDayTotalCollectFlowListByFtpId(ftpId, timeDay);
        DecimalFormat df = new DecimalFormat("#0.00");
        if (dbCollectList != null && dbCollectList.size( ) > 0) {
            inTotal = dbCollectList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
            long total = inTotal;
            dbCollectList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
            dbCollectList.forEach(p -> {
                long num = Long.parseLong(p.getNum( ));
                double per = num * 100.00 / total;
                String perStr = df.format(per);
                p.setPer(perStr);
            });
            ftpFlv.setCollectNums(dbCollectList);
        }
        long outTotal = 0;
        //资源采集量
        List<FTPFlowStatPojo> dbResrcList = ftpFlowStatisticMapper.findDayTotalResrcFlowListByFtpId(ftpId, timeDay);
        if (dbResrcList != null && dbResrcList.size( ) > 0) {
            outTotal = dbResrcList.stream( ).mapToLong(p -> Long.parseLong(p.getNum( ))).sum( );
            dbResrcList.sort((v1, v2) -> v2.getNum( ).compareTo(v1.getNum( )));
            long total = outTotal;
            dbResrcList.forEach(p -> {
                long num = Long.parseLong(p.getNum( ));
                double per = num * 100.00 / total;
                String perStr = df.format(per);
                p.setPer(perStr);
            });
            ftpFlv.setDataBaseNums(dbResrcList);
        }
        FTPFlowPointVo pointVo = new FTPFlowPointVo( );
        pointVo.setName(name);
        pointVo.setId(ftpId);
        long flowTotal = inTotal + outTotal;
        double inTPer = inTotal * 100.00 / flowTotal;
        double outTPer = 100 - inTPer;
        pointVo.setInPer(df.format(inTPer));
        pointVo.setOutPer(df.format(outTPer));
        pointVo.setInNum(String.valueOf(inTotal));
        pointVo.setOutNum(String.valueOf(outTotal));
        ftpFlv.setTotal(pointVo);
        return ftpFlv;
    }

    @Override
    public List<KeyAndValueVo> findAllFtpClients() {
        List<KeyAndValueVo> ret = logQueryMapper.findResourcesByResTypeCode(ResLogType.FTPCJRZ.getValue( ));
        return ret;
    }

    @Override
    public Map<String, Object> findFtpHourBarGraph(String ftpId, String timeDay) {
        String currentDayStr = DateUtils.format(new Date( ), DateUtils.DATE_FORMAT_1);
        if (!StringUtils.isNotBlank(timeDay)) {
            timeDay = currentDayStr;
        }
        timeDay = timeDay.trim( );
        //采集端采集量
        List<FTPFlowStatPojo> dbCollectList = ftpFlowStatisticMapper.findHourCollectFlowListByFtpId(ftpId, timeDay);
        //资源采集量
        List<FTPFlowStatPojo> dbResrcList = ftpFlowStatisticMapper.findHourResrcFlowListByFtpId(ftpId, timeDay);
        int hourNum = 24;
        if (timeDay.equals(currentDayStr)) {
            hourNum = DateUtils.getHour(new Date( ));
        }
        List<String> inL = dbCollectList.stream( ).map(p -> p.getName( )).distinct( ).collect(Collectors.toList( ));
        int statNum = hourNum;
        List<Map<String, Object>> inLegendList = inL.stream( ).map(p -> {
            Map<String, Object> legend = new HashMap<String, Object>( );
            legend.put("name", p);
            legend.put("stack", "流入");
            List<String> dataList = Lists.newArrayList( );
            for (int i = 0; i < statNum; i++) {
                dataList.add("0");
            }
            legend.put("data", dataList);
            return legend;
        }).collect(Collectors.toList( ));
        List<String> outL = dbResrcList.stream( ).map(p -> p.getName( )).distinct( ).collect(Collectors.toList( ));
        List<Map<String, Object>> outLegendList = outL.stream( ).map(p -> {
            Map<String, Object> legend = new HashMap<String, Object>( );
            legend.put("name", p);
            legend.put("stack", "流出");
            List<String> dataList = Lists.newArrayList( );
            for (int i = 0; i < statNum; i++) {
                dataList.add("0");
            }
            legend.put("data", dataList);
            return legend;
        }).collect(Collectors.toList( ));
        List<String> xAxisList = Lists.newArrayList( );
        for (int i = 0; i < hourNum; i++) {
            xAxisList.add(String.valueOf(i));
            String starTime = getTimeStr(i);
            String dbTime = timeDay + " " + starTime;
            for (Map<String, Object> p : inLegendList) {
                String name = String.valueOf(p.get("name"));
                List<String> dataList = (List<String>) p.get("data");
                List<FTPFlowStatPojo> statInPojoList = dbCollectList.stream( ).filter(k -> name.equals(k.getName( )) && dbTime.equals(k.getStatTime( ))).collect(Collectors.toList( ));
                if (statInPojoList != null && statInPojoList.size( ) > 0) {
                    dataList.remove(i);
                    dataList.add(i, statInPojoList.get(0).getNum( ));
                }
                p.put("data", dataList);
            }
            for (Map<String, Object> p : outLegendList) {
                String name = String.valueOf(p.get("name"));
                List<String> dataList = (List<String>) p.get("data");
                List<FTPFlowStatPojo> statOutPojoList = dbResrcList.stream( ).filter(k -> name.equals(k.getName( )) && dbTime.equals(k.getStatTime( ))).collect(Collectors.toList( ));
                if (statOutPojoList != null && statOutPojoList.size( ) > 0) {
                    dataList.remove(i);
                    dataList.add(i, statOutPojoList.get(0).getNum( ));
                }
                p.put("data", dataList);
            }
        }
        Map<String, Object> ret = new HashMap<>( );
        ret.put("xAxis", xAxisList);
        List<Map<String, Object>> dataList = Lists.newArrayList( );
        dataList.addAll(inLegendList);
        dataList.addAll(outLegendList);
        ret.put("datas", dataList);
        List<String> legendList = Lists.newArrayList( );
        legendList.addAll(inL);
        legendList.addAll(outL);
        ret.put("legend", legendList);
        return ret;
    }

    @Override
    public Map<String, Object> findFtpDayBarGraph(String ftpId, String startDay, String endDay) {
        String beginDate, endDate;//起始 与终止 用于向数据库传输
        int dayBw = 30;
        if (StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay)) {
            beginDate = startDay;
            endDate = endDay;
            dayBw = DateUtils.daysBetween(DateUtils.parse(beginDate, DateUtils.DATE_FORMAT_1), DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1));
            //如果结束时间不为空起始时间为空默认取终止时间7天前的日期
        } else if (!StringUtils.isNotBlank(startDay) && StringUtils.isNotBlank(endDay)) {
            endDate = endDay;
            beginDate = DateUtils.format(DateUtils.getDateBefore(DateUtils.parse(endDay, DateUtils.DATE_FORMAT_1), 30), DateUtils.DATE_FORMAT_1);
            //如果终止时间为空，起始时间不为空
        } else if (StringUtils.isNotBlank(startDay) && !StringUtils.isNotBlank(endDay)) {
            beginDate = startDay;
            int num = DateUtils.daysBetween(DateUtils.parse(beginDate, DateUtils.DATE_FORMAT_1), new Date( ));
            if (num > 30) {
                endDate = DateUtils.format(DateUtils.getDateBefore(DateUtils.parse(startDay, DateUtils.DATE_FORMAT_1), -30), DateUtils.DATE_FORMAT_1);
            } else {
                dayBw = num;
                endDate = DateUtils.format(new Date( ), DateUtils.DATE_FORMAT_1);
            }
        } else {
            beginDate = DateUtils.format(DateUtils.getDateBefore(new Date( ), 31), DateUtils.DATE_FORMAT_1);
            endDate = DateUtils.format(DateUtils.getDateBefore(new Date( ), 1), DateUtils.DATE_FORMAT_1);
        }
        //采集端采集量
        List<FTPFlowStatPojo> dbCollectList = ftpFlowStatisticMapper.findDayCollectFlowListByFtpId(ftpId, beginDate, endDate);
        //资源采集量
        List<FTPFlowStatPojo> dbResrcList = ftpFlowStatisticMapper.findDayResrcFlowListByFtpId(ftpId, beginDate, endDate);
        List<String> inL = dbCollectList.stream( ).map(p -> p.getName( )).distinct( ).collect(Collectors.toList( ));
        int statNum = dayBw;
        List<Map<String, Object>> inLegendList = inL.stream( ).map(p -> {
            Map<String, Object> legend = new HashMap<String, Object>( );
            legend.put("name", p);
            legend.put("stack", "流入");
            List<String> dataList = Lists.newArrayList( );
            for (int i = 0; i < statNum; i++) {
                dataList.add("0");
            }
            legend.put("data", dataList);
            return legend;
        }).collect(Collectors.toList( ));
        List<String> outL = dbResrcList.stream( ).map(p -> p.getName( )).distinct( ).collect(Collectors.toList( ));
        List<Map<String, Object>> outLegendList = outL.stream( ).map(p -> {
            Map<String, Object> legend = new HashMap<String, Object>( );
            legend.put("name", p);
            legend.put("stack", "流出");
            List<String> dataList = Lists.newArrayList( );
            for (int i = 0; i < statNum; i++) {
                dataList.add("0");
            }
            legend.put("data", dataList);
            return legend;
        }).collect(Collectors.toList( ));
        List<String> xAxisList = Lists.newArrayList( );
        Date endD = DateUtils.parse(endDate, DateUtils.DATE_FORMAT_1);
        for (int i = dayBw; i > 0; i--) {
            String dbTime = DateUtils.format(DateUtils.getDateBefore(endD, i - 1), DateUtils.DATE_FORMAT_1);
            xAxisList.add(dbTime);
            for (Map<String, Object> p : inLegendList) {
                String name = String.valueOf(p.get("name"));
                List<String> dataList = (List<String>) p.get("data");
                List<FTPFlowStatPojo> statInPojoList = dbCollectList.stream( ).filter(k -> name.equals(k.getName( )) && dbTime.equals(k.getStatTime( ))).collect(Collectors.toList( ));
                if (statInPojoList != null && statInPojoList.size( ) > 0) {
                    dataList.remove(30 - i);
                    dataList.add(30 - i, statInPojoList.get(0).getNum( ));
                }
                p.put("data", dataList);
            }
            for (Map<String, Object> p : outLegendList) {
                String name = String.valueOf(p.get("name"));
                List<String> dataList = (List<String>) p.get("data");
                List<FTPFlowStatPojo> statOutPojoList = dbResrcList.stream( ).filter(k -> name.equals(k.getName( )) && dbTime.equals(k.getStatTime( ))).collect(Collectors.toList( ));
                if (statOutPojoList != null && statOutPojoList.size( ) > 0) {
                    dataList.remove(30 - i);
                    dataList.add(30 - i, statOutPojoList.get(0).getNum( ));
                }
                p.put("data", dataList);
            }
        }
        Map<String, Object> ret = new HashMap<>( );
        ret.put("xAxis", xAxisList);
        List<Map<String, Object>> dataList = Lists.newArrayList( );
        dataList.addAll(inLegendList);
        dataList.addAll(outLegendList);
        ret.put("datas", dataList);
        List<String> legendList = Lists.newArrayList( );
        legendList.addAll(inL);
        legendList.addAll(outL);
        ret.put("legend", legendList);
        return ret;
    }

    public Map<String, List<Map<String, String>>> statFlowWarnInfo(List<FlowWarnPojo> dbFlowWarnInfo) {
        //剔除未配置的告警规则的数据
        List<FlowWarnPojo> existRuleList = dbFlowWarnInfo.stream( ).filter(p -> StringUtils.isNotBlank(p.getInLimit( )) || StringUtils.isNotBlank(p.getOutLimit( ))).collect(Collectors.toList( ));
        //找出只有IP地址的信息,
        List<FlowWarnPojo> dbIpList = existRuleList.stream( ).filter(p -> p.getClusterId( ).equals("1000")).collect(Collectors.toList( ));
        List<FlowWarnPojo> newDbIpList = dbIpList.stream( ).map(p -> {
            FlowWarnPojo newP = new FlowWarnPojo( );
            try {
                BeanUtils.copyProperties(newP, p);
            } catch (IllegalAccessException e) {
                e.printStackTrace( );
            } catch (InvocationTargetException e) {
                e.printStackTrace( );
            }
            newP.setClusterId(p.getClientIp( ));
            return newP;
        }).collect(Collectors.toList( ));
        //找出集群的信息
        List<FlowWarnPojo> clusterList = existRuleList.stream( ).filter(p -> !p.getClusterId( ).equals("1000")).collect(Collectors.toList( ));
        clusterList.addAll(newDbIpList);
        Map<String, List<Map<String, String>>> ret = new HashMap<>( );
        //首先找出集群的告警规则
        for (int i = 0; i < clusterList.size( ); i++) {
            FlowWarnPojo warnPojo = clusterList.get(i);
            String cId = warnPojo.getClusterId( );
            List<Map<String, String>> warnIpList = ret.get(cId);
            if (warnIpList != null) {
                //找出in超过上限与下限的警告
                //找出in超过上限与下限的警告
                boolean isInLimit = false;
                if (StringUtils.isNotBlank(warnPojo.getInLimit( ))) {
                    String[] limitArr = warnPojo.getInLimit( ).split(",");
                    if (Long.parseLong(limitArr[0]) > warnPojo.getInNum( )) {
                        isInLimit = true;
                    } else {
                        if (!"MAX".equals(limitArr[1])) {
                            if (warnPojo.getInNum( ) >= Long.parseLong(limitArr[1])) {
                                isInLimit = true;
                            }
                        }
                    }
                }

                //找出out超过上限与下限的警告
                boolean isOutLimit = false;
                if (StringUtils.isNotBlank(warnPojo.getOutLimit( ))) {
                    String[] limitArr = warnPojo.getOutLimit( ).split(",");
                    if (Long.parseLong(limitArr[0]) > warnPojo.getOutNum( )) {
                        isOutLimit = true;
                    } else {
                        if (!"MAX".equals(limitArr[1])) {
                            if (warnPojo.getOutNum( ) >= Long.parseLong(limitArr[1])) {
                                isOutLimit = true;
                            }
                        }
                    }
                }
                if (isOutLimit || isInLimit) {
                    Map<String, String> limitMap = new HashMap<>( );
                    limitMap.put("clientIp", warnPojo.getClientIp( ));
                    limitMap.put("inNum", String.valueOf(warnPojo.getInNum( )));
                    limitMap.put("outNum", String.valueOf(warnPojo.getOutNum( )));
                    limitMap.put("inLimit", warnPojo.getInLimit( ));
                    limitMap.put("outLimit", warnPojo.getOutLimit( ));
                    limitMap.put("inStatus", isInLimit == false ? "正常" : "异常");
                    limitMap.put("outStatus", isOutLimit == false ? "正常" : "异常");
                    warnIpList.add(limitMap);
                    ret.put(warnPojo.getClusterId( ), warnIpList);
                }

            } else {
                //找出in超过上限与下限的警告
                boolean isInLimit = false;
                if (StringUtils.isNotBlank(warnPojo.getInLimit( ))) {
                    String[] limitArr = warnPojo.getInLimit( ).split(",");
                    if (Long.parseLong(limitArr[0]) > warnPojo.getInNum( )) {
                        isInLimit = true;
                    } else {
                        if (!"MAX".equals(limitArr[1])) {
                            if (warnPojo.getInNum( ) >= Long.parseLong(limitArr[1])) {
                                isInLimit = true;
                            }
                        }
                    }
                }
                //找出out超过上限与下限的警告
                boolean isOutLimit = false;
                if (StringUtils.isNotBlank(warnPojo.getOutLimit( ))) {
                    String[] limitArr = warnPojo.getOutLimit( ).split(",");
                    if (Long.parseLong(limitArr[0]) > warnPojo.getOutNum( )) {
                        isOutLimit = true;
                    } else {
                        if (!"MAX".equals(limitArr[1])) {
                            if (warnPojo.getOutNum( ) >= Long.parseLong(limitArr[1])) {
                                isOutLimit = true;
                            }
                        }
                    }
                }
                if (isOutLimit || isInLimit) {
                    warnIpList = Lists.newArrayList( );
                    Map<String, String> limitMap = new HashMap<>( );
                    limitMap.put("clientIp", warnPojo.getClientIp( ));
                    limitMap.put("inNum", String.valueOf(warnPojo.getInNum( )));
                    limitMap.put("outNum", String.valueOf(warnPojo.getOutNum( )));
                    limitMap.put("inLimit", warnPojo.getInLimit( ));
                    limitMap.put("outLimit", warnPojo.getOutLimit( ));
                    limitMap.put("inStatus", isInLimit == false ? "正常" : "异常");
                    limitMap.put("outStatus", isOutLimit == false ? "正常" : "异常");
                    warnIpList.add(limitMap);
                    ret.put(warnPojo.getClusterId( ), warnIpList);
                }
            }

        }
        return ret;
    }

    @Override
    public Map<String, Object> statFlowSvgGraph(HttpServletRequest request) {
//        数据库资源与客户端之间的实时流向基础数据
        List<FlowStatPojo> dbRescRafficList = ftpFlowStatisticMapper.findRafficDirecResrcList( );
        //应用系统与客户端之间的实时流向基础数据
        List<FlowStatPojo> dbAppRafficList = ftpFlowStatisticMapper.findRafficDirecAppList( );
        //排序的顺序
        List<FlowStatOrderPojo> orderList = ftpFlowStatisticMapper.findFlowStatOrderList( );

        //取出资源的告警
        List<FlowWarnPojo> dbResFlowWarnInfo = ftpFlowStatisticMapper.findResFlowWarnInfo( );
        // 统计出是否有资源告警
        Map<String, List<Map<String, String>>> resStatResult = statFlowWarnInfo(dbResFlowWarnInfo);

        //取出APP的告警
        List<FlowWarnPojo> dbAppFlowWarnInfo = ftpFlowStatisticMapper.findAppFlowWarnInfo( );
        // 统计出是否有APP告警,只针对ftp的情况
        Map<String, List<Map<String, String>>> appStatResult = statFlowWarnInfo(dbAppFlowWarnInfo);
        String resUrl = RequestUtil.getCurrentWebRootUrl(request);
        Map<String, List<Map<String, Object>>> resMap = new HashMap<>( );
        Map<String, List<Map<String, Object>>> sysMap = new HashMap<>( );
        //连线的数据
        List<Map<String, Object>> lineDatas = Lists.newArrayList( );
        //转换数据
        for (int i = 0; i < dbRescRafficList.size( ); i++) {
            FlowStatPojo singlePojo = dbRescRafficList.get(i);
            List<Map<String, Object>> oldMapList = resMap.get(singlePojo.getResrcType( ));
            //资源图片
            String resImageUrl = resUrl + FlowStatImageUrlEnum.getImageUrl(singlePojo.getResrcType( ) + "_2");
            String resName = singlePojo.getResrcName( );
            //开始对资源进行分组
            Map<String, Object> resSingle = new HashMap<>( );
            resSingle.put("url", resImageUrl);
            resSingle.put("name", resName);
            resSingle.put("id", singlePojo.getResrcId( ));
            //判断资源是否存在
            if (oldMapList != null) {
                //对资源进行过滤，去除重复的数据
                List<Map<String, Object>> currentList = oldMapList.stream( ).filter(p -> singlePojo.getResrcId( ).equals(p.get("id"))).collect(Collectors.toList( ));
                if (currentList == null || currentList.size( ) == 0) {
                    oldMapList.add(resSingle);
                }
            } else {
                oldMapList = Lists.newArrayList( );
                oldMapList.add(resSingle);
            }
            resMap.put(singlePojo.getResrcType( ), oldMapList);
            /////////////////////////华丽的分隔线/////// 此处组装或过滤集群数据////////////////////////////////////////
            //开始对集群进行分组过滤
            //集群图片
            String sysTypeName = singlePojo.getClientType( );
            String sysPyTypeName = Pinyin4jUtil.converterToFirstSpellOnly(sysTypeName);
            String resType = singlePojo.getResrcType( );
            //拼接集群显示的图片
            String sysImageUrl = resUrl + FlowStatImageUrlEnum.getImageUrl(sysPyTypeName);
            Map<String, Object> sysSingle = new HashMap<>( );
            sysSingle.put("url", sysImageUrl);
            sysSingle.put("name", singlePojo.getClientName( ));
            sysSingle.put("id", singlePojo.getClientId( ));
            //取出流量
            long sourceNum = singlePojo.getInNum( );
            long targetNum = singlePojo.getOutNum( );
            //找出集群的类型
            List<Map<String, Object>> oldSysMapList = sysMap.get(sysTypeName);
            if (oldSysMapList != null) {
                //对集群进行过滤，去除重复的数据
                List<Map<String, Object>> currentList = oldSysMapList.stream( ).filter(p -> singlePojo.getClientId( ).equals(p.get("id"))).collect(Collectors.toList( ));
                if (currentList == null || currentList.size( ) == 0) {
                    List<Map<String, Object>> set = Lists.newArrayList( );
                    Map<String, Object> singleFlowNumToMap = new HashMap<>( );
                    singleFlowNumToMap.put("name", resType);
                    singleFlowNumToMap.put("flowTop", sourceNum);
                    singleFlowNumToMap.put("flowDown", targetNum);
                    singleFlowNumToMap.put("nameHeard", resName);
                    set.add(singleFlowNumToMap);
                    sysSingle.put("to", set);
                    oldSysMapList.add(sysSingle);
                } else {
                    //当存在的当前集群对象的时候，需要判断to 资源的类型是否存在
                    Map<String, Object> oldSysSingle = currentList.get(0);
                    List<Map<String, Object>> set = (List<Map<String, Object>>) oldSysSingle.get("to");
                    //找出类型是否存在，
                    List<Map<String, Object>> singleSet = set.stream( ).filter(p -> resType.equals(p.get("nameHeard"))).collect(Collectors.toList( ));
                    //不存在就需要将to 类型重新加载Map中
                    if (singleSet == null || singleSet.size( ) == 0) {
                        Map<String, Object> singleFlowNumToMap = new HashMap<>( );
                        singleFlowNumToMap.put("name", resType);
                        singleFlowNumToMap.put("flowTop", sourceNum);
                        singleFlowNumToMap.put("flowDown", targetNum);
                        singleFlowNumToMap.put("nameHeard", resName);
                        set.add(singleFlowNumToMap);
                        oldSysSingle.put("to", set);
                        oldSysMapList.removeAll(currentList);
                        oldSysMapList.add(oldSysSingle);
                    } else {
                        //如果存在，需要将下行流量与上行流量的值重新计算添加到一起
                        Map<String, Object> singleFlowNumToMap = singleSet.get(0);
                        long newSourceNum = Long.parseLong(String.valueOf(singleFlowNumToMap.get("flowTop"))) + sourceNum;
                        long newTargetNum = Long.parseLong(String.valueOf(singleFlowNumToMap.get("flowDown"))) + targetNum;
                        singleFlowNumToMap.put("flowTop", newSourceNum);
                        singleFlowNumToMap.put("flowDown", newTargetNum);
                        set.add(singleFlowNumToMap);
                        oldSysSingle.put("to", set);
                        oldSysMapList.removeAll(currentList);
                        oldSysMapList.add(oldSysSingle);
                    }
                }
            } else {
                Map<String, Object> singleFlowNumToMap = new HashMap<>( );
                oldSysMapList = Lists.newArrayList( );
                List<Map<String, Object>> set = Lists.newArrayList( );
                singleFlowNumToMap.put("name", resType);
                singleFlowNumToMap.put("flowTop", sourceNum);
                singleFlowNumToMap.put("flowDown", targetNum);
                singleFlowNumToMap.put("nameHeard", resName);
                set.add(singleFlowNumToMap);
                sysSingle.put("to", set);
                oldSysMapList.add(sysSingle);
            }
            //然后需要将数组缓存到数据库中
            sysMap.put(sysTypeName, oldSysMapList);
            //组装连线的数据
            Map<String, Object> singleLine = new HashMap<>( );
            singleLine.put("source", singlePojo.getResrcType( ));
            singleLine.put("target", singlePojo.getClientType( ));
            singleLine.put("sourceType", resType);
            singleLine.put("targetType", sysPyTypeName);
            singleLine.put("sourceFlow", targetNum);
            singleLine.put("targetFlow", sourceNum);
            lineDatas.add(singleLine);
        }
        //开始组装dataHeard数据
        List<Map<String, Object>> dataHeardList = Lists.newArrayList( );
        List<String> resTypeList = Lists.newArrayList( );
        Set<String> targetTypeSet = new HashSet<>( );
        //开始将资源头部数据进行组装，并添加资源的图标
        for (Map.Entry<String, List<Map<String, Object>>> entry : resMap.entrySet( )) {
            Map<String, Object> singleHeard = new HashMap<>( );
            String typeName = entry.getKey( );
            String typeImageUrl = resUrl + FlowStatImageUrlEnum.getImageUrl(typeName);
            List<FlowStatOrderPojo> singleOrderList = orderList.stream( ).filter(p -> typeName.equals(p.getValue( )) && 1 == p.getType( ).intValue( )).collect(Collectors.toList( ));
            int resFlag = 99;
            if (singleOrderList != null && singleOrderList.size( ) > 0) {
                resFlag = singleOrderList.get(0).getOrder( ).intValue( );
            }
            singleHeard.put("index", resFlag);
            String typeStr = "A_" + typeName + "_" + resFlag;
            targetTypeSet.add(typeStr);
            for (int i = 0; i < lineDatas.size( ); i++) {
                Map<String, Object> p = lineDatas.get(i);
                if (typeName.equals(String.valueOf(p.get("sourceType")))) {
                    p.put("sourceType", typeStr);
                }
                lineDatas.remove(i);
                lineDatas.add(i, p);
            }
            singleHeard.put("type", typeStr);
            singleHeard.put("name", typeName);
            singleHeard.put("url", typeImageUrl);
            singleHeard.put("child", entry.getValue( ));
            resTypeList.add(typeStr);
            dataHeardList.add(singleHeard);
        }
        String lineSource = resUrl + FlowStatImageUrlEnum.IMAGE_PRO_LINE_TARGET;
        String lineTarget = resUrl + FlowStatImageUrlEnum.IMAGE_PRO_LINE_SOURCE;
        String sysUrl = resUrl + FlowStatImageUrlEnum.getImageUrl(FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ));
        //开始找FTP集群的数据
        Map<String, Map<String, Object>> appMap = new HashMap<>( );
        //客户端的资源
        Map<String, Map<String, Object>> resAppMap = new HashMap<>( );
        String ftpImgUrl = resUrl + FlowStatImageUrlEnum.getImageUrl(FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ));
        for (int i = 0; i < dbAppRafficList.size( ); i++) {
            FlowStatPojo currentPojo = dbAppRafficList.get(i);
            long sourceNum = currentPojo.getInNum( );
            long targetNum = currentPojo.getOutNum( );
            String clientName = currentPojo.getClientName( );
            Map<String, Object> oldApp = appMap.get(currentPojo.getClientName( ));
            String namePy = Pinyin4jUtil.converterToFirstSpellOnly(clientName);
            String typeStr = "C_" + namePy;
            if (oldApp == null) {
                oldApp = new HashMap<>( );
                oldApp.put("type", typeStr);
                oldApp.put("sourceFlow", sourceNum);
                oldApp.put("targetFlow", targetNum);
                oldApp.put("sourceImage", lineSource);
                oldApp.put("targetImage", lineTarget);
                oldApp.put("name", clientName);
                oldApp.put("id", currentPojo.getClientId( ));
                oldApp.put("url", sysUrl);
            } else {
                long oldSourceNum = Long.parseLong(String.valueOf(oldApp.get("sourceFlow")));
                oldSourceNum += sourceNum;
                long oldTargetNum = Long.parseLong(String.valueOf(oldApp.get("targetFlow")));
                oldTargetNum += targetNum;
                oldApp.put("sourceFlow", oldSourceNum);
                oldApp.put("targetFlow", oldTargetNum);
            }
            appMap.put(clientName, oldApp);
            String resId = currentPojo.getResrcId( );
            String resName = currentPojo.getResrcName( );
            Map<String, Object> oldResCAppMap = resAppMap.get(resName);
            //求出c端的资源
            if (oldResCAppMap != null) {
                //对集群进行过滤，去除重复的数据
                List<Map<String, Object>> oldSysMapList = (List<Map<String, Object>>) oldResCAppMap.get("toFTP");
                List<Map<String, Object>> currentList = oldSysMapList.stream( ).filter(p -> typeStr.equals(p.get("name"))).collect(Collectors.toList( ));
                if (currentList == null || currentList.size( ) == 0) {
                    Map<String, Object> singleFlowNumToMap = new HashMap<>( );
                    singleFlowNumToMap.put("flowTop", sourceNum);
                    singleFlowNumToMap.put("flowDown", targetNum);
                    singleFlowNumToMap.put("name", typeStr);
                    oldSysMapList.add(singleFlowNumToMap);
                    oldResCAppMap.put("toFTP", oldSysMapList);
                } else {
                    Map<String, Object> singleFlowNumToMap = currentList.get(0);
                    long newSourceNum = Long.parseLong(String.valueOf(singleFlowNumToMap.get("flowTop"))) + sourceNum;
                    long newTargetNum = Long.parseLong(String.valueOf(singleFlowNumToMap.get("flowDown"))) + targetNum;
                    singleFlowNumToMap.put("flowTop", newSourceNum);
                    singleFlowNumToMap.put("flowDown", newTargetNum);
                    oldSysMapList.removeAll(currentList);
                    oldSysMapList.add(singleFlowNumToMap);
                }
            } else {
                oldResCAppMap = new HashMap<>( );
                oldResCAppMap.put("id", resId);
                oldResCAppMap.put("name", resName);
                oldResCAppMap.put("url", ftpImgUrl);
                List<Map<String, Object>> singleTpList = Lists.newArrayList( );
                Map<String, Object> singleFlowNumToMap = new HashMap<>( );
                singleFlowNumToMap.put("flowTop", sourceNum);
                singleFlowNumToMap.put("flowDown", targetNum);
                singleFlowNumToMap.put("name", typeStr);
                singleTpList.add(singleFlowNumToMap);
                oldResCAppMap.put("from", typeStr);
                oldResCAppMap.put("to", Lists.newArrayList( ));
                oldResCAppMap.put("toFTP", singleTpList);
            }
            resAppMap.put(resName, oldResCAppMap);
        }
        List<Map<String, Object>> ftpChildren = appMap.values( ).stream( ).collect(Collectors.toList( ));
        //组装最后的结果
        Map<String, Object> FTPData = new HashMap<>( );

        FTPData.put("from", "FTP");
        FTPData.put("icon", sysUrl);
        FTPData.put("only", "only");
        FTPData.put("children", ftpChildren);//
        //组装采集的结果
        List<Map<String, Object>> existChildren = Lists.newArrayList( );
        List<Map<String, Object>> allCdFtpList = resAppMap.values( ).stream( ).collect(Collectors.toList( ));
        String sysIconUrl = resUrl + FlowStatImageUrlEnum.IMAGE_PRO_SYS_DOWN;
        String ftpType = "";
        String ftpName = "";
        String warnIconUrl = resUrl + FlowStatImageUrlEnum.IMAGE_FLOW_WARN;
        List<Map<String, Object>> mindDataList = Lists.newArrayList( );
        for (Map.Entry<String, List<Map<String, Object>>> entry : sysMap.entrySet( )) {
            Map<String, Object> singleMid = new HashMap<>( );
            String typeName = entry.getKey( );
            String sysPyTypeName = Pinyin4jUtil.converterToFirstSpellOnly(typeName);
            List<FlowStatOrderPojo> singleOrderList = orderList.stream( ).filter(p -> typeName.equals(p.getValue( )) && 2 == p.getType( ).intValue( )).collect(Collectors.toList( ));
            int sysFlag = 99;
            if (singleOrderList != null && singleOrderList.size( ) > 0) {
                sysFlag = singleOrderList.get(0).getOrder( ).intValue( );
            }
            singleMid.put("index", sysFlag);
            String typeStr = "B_" + sysPyTypeName + "_" + sysFlag;
            for (int i = 0; i < lineDatas.size( ); i++) {
                Map<String, Object> p = lineDatas.get(i);
                if (sysPyTypeName.equals(String.valueOf(p.get("targetType")))) {
                    p.put("targetType", typeStr);
                }
                lineDatas.remove(i);
                lineDatas.add(i, p);
            }
            List<Map<String, Object>> entryList = entry.getValue( );
            entryList.forEach(p -> {
                List<Map<String, Object>> toSingleList = (List<Map<String, Object>>) p.get("to");
                List<Map<String, Object>> newToSingleList = toSingleList.stream( ).map(k -> {
                    List<String> currentTypeNameList = resTypeList.stream( ).filter(h -> h.contains(String.valueOf(k.get("name")))).collect(Collectors.toList( ));
                    String currentTypeName = "";
                    if (currentTypeNameList != null && currentTypeNameList.size( ) > 0) {
                        currentTypeName = currentTypeNameList.get(0);
                    }
                    k.put("name", currentTypeName);
                    k.put("flowType", currentTypeName);
                    return k;
                }).filter(k -> StringUtils.isNotBlank(String.valueOf(k.get("name")))).distinct( ).collect(Collectors.toList( ));
                //找出toFtp的值
                String clientId = String.valueOf(p.get("id"));
                if (FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ).equals(typeName)) {
                    List<Map<String, Object>> currentFtpChildren = allCdFtpList.stream( ).filter(h -> clientId.equals(h.get("id"))).collect(Collectors.toList( ));
                    if (currentFtpChildren != null && currentFtpChildren.size( ) > 0) {
                        existChildren.addAll(currentFtpChildren);
                        p.put("toFTP", currentFtpChildren.get(0).get("toFTP"));
                    } else {
                        p.put("toFTP", Lists.newArrayList( ));
                    }
                }
                List<Map<String, String>> warnResIpList = resStatResult.get(clientId);
                p.put("sort", 0);
                if (warnResIpList != null && warnResIpList.size( ) > 0) {
                    p.put("alarmUrl", warnIconUrl);
                    p.put("message", warnResIpList);
                    p.put("sort", 1);
                } else {
                    p.put("message", Lists.newArrayList( ));
                }
                p.put("from", typeStr);
                p.put("to", newToSingleList);
            });
            singleMid.put("type", typeStr);
            singleMid.put("name", typeName);
            singleMid.put("url", sysIconUrl);
            singleMid.put("icon", sysIconUrl);
            //对FTP进行特殊处理，添加只有tfp的节点，告警的规则方式不同
            if (FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ).equals(typeName)) {
                ftpType = typeStr;
                ftpName = typeName;
                allCdFtpList.removeAll(existChildren);
                entryList.forEach(u -> {
                    String id = String.valueOf(u.get("id"));
                    List<Map<String, String>> msgList = (List<Map<String, String>>) u.get("message");
                    List<Map<String, String>> warnAppIpList = appStatResult.get(id);
                    if (msgList != null && msgList.size( ) > 0) {
                        msgList.forEach(z -> {
                            if (warnAppIpList != null && warnAppIpList.size( ) > 0) {
                                String clientIp = z.get("clientIp");
                                List<Map<String, String>> singleAppList = warnAppIpList.stream( ).filter(h -> clientIp.equals(h.get("clientIp"))).collect(Collectors.toList( ));
                                if (singleAppList != null && singleAppList.size( ) > 0) {
                                    Map<String, String> appWarnSingle = singleAppList.get(0);
                                    z.put("inNum", appWarnSingle.get("inNum"));
                                    z.put("inLimit", appWarnSingle.get("inLimit"));
                                    z.put("inStatus", appWarnSingle.get("inStatus"));
                                }
                            }
                        });
                    } else {
                        u.put("sort", 0);
                        if (warnAppIpList != null && warnAppIpList.size( ) > 0) {
                            u.put("alarmUrl", warnIconUrl);
                            u.put("message", warnAppIpList);
                            u.put("sort", 1);
                        } else {
                            u.put("message", Lists.newArrayList( ));
                        }
                    }
                });
                List<Map<String, Object>> otherAllFTpList = allCdFtpList.stream( ).map(p -> {
                    Map<String, Object> newFtp = new HashMap<>( );
                    String id = String.valueOf(p.get("id"));
                    newFtp.put("id", id);
                    newFtp.put("name", p.get("name"));
                    newFtp.put("url", p.get("url"));
                    List<Map<String, Object>> singleTpList = (List<Map<String, Object>>) p.get("toFTP");
                    newFtp.put("from", typeStr);
                    newFtp.put("to", Lists.newArrayList( ));
                    newFtp.put("toFTP", singleTpList);
                    List<Map<String, String>> warnAppIpList = appStatResult.get(id);
                    if (warnAppIpList != null && warnAppIpList.size( ) > 0) {
                        newFtp.put("alarmUrl", warnIconUrl);
                        newFtp.put("message", warnAppIpList);
                    }
                    return newFtp;
                }).collect(Collectors.toList( ));
                entryList.addAll(otherAllFTpList);
            }
            entryList.sort((v1, v2) -> String.valueOf(v2.get("sort")).compareTo(String.valueOf(v1.get("sort"))));
            singleMid.put("child", entryList);
            mindDataList.add(singleMid);
        }
        //在中间层没有FTP时需要将下层的FTP数据加载到中间层中
        List<Map<String, Object>> ftpMidList = mindDataList.stream( ).filter(p -> String.valueOf(p.get("type")).contains(FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ))).collect(Collectors.toList( ));
        if (dbAppRafficList != null && dbAppRafficList.size( ) > 0 && (ftpMidList == null || ftpMidList.size( ) == 0)) {
            Map<String, Object> singleMid = new HashMap<>( );
            int sysFlag = 99;
            List<FlowStatOrderPojo> singleOrderList = orderList.stream( ).filter(p -> FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ).equals(p.getValue( )) && 2 == p.getType( ).intValue( )).collect(Collectors.toList( ));
            if (singleOrderList != null && singleOrderList.size( ) > 0) {
                sysFlag = singleOrderList.get(0).getOrder( ).intValue( );
            }
            singleMid.put("index", sysFlag);
            String typeStr = "B_" + FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ) + "_" + sysFlag;
            singleMid.put("type", typeStr);
            singleMid.put("name", FlowStatImageUrlEnum.APP_FLAG_FTP.getTypeName( ));
            singleMid.put("url", sysIconUrl);
            singleMid.put("icon", sysIconUrl);
            List<Map<String, Object>> otherAllFTpList = allCdFtpList.stream( ).map(p -> {
                Map<String, Object> newFtp = new HashMap<>( );
                newFtp.put("id", p.get("id"));
                newFtp.put("name", p.get("name"));
                newFtp.put("url", p.get("url"));
                List<Map<String, Object>> singleTpList = (List<Map<String, Object>>) p.get("toFTP");
                newFtp.put("from", typeStr);
                newFtp.put("to", Lists.newArrayList( ));
                newFtp.put("toFTP", singleTpList);
                return newFtp;
            }).collect(Collectors.toList( ));
            singleMid.put("child", otherAllFTpList);
            mindDataList.add(singleMid);
        }
        FTPData.put("type", ftpType);
        FTPData.put("name", "FTP_CJY_ALL");
        Map<String, Map<String, Object>> lineDataMap = new HashMap<>( );
        for (int i = 0; i < lineDatas.size( ); i++) {
            Map<String, Object> singleMap = lineDatas.get(i);
            String sourceType = String.valueOf(singleMap.get("sourceType"));
            String targetType = String.valueOf(singleMap.get("targetType"));
            String keyType = sourceType + targetType;
            Map<String, Object> oldMap = lineDataMap.get(keyType);
            long sourceFlow = Long.valueOf(String.valueOf(singleMap.get("sourceFlow")));
            long targetFlow = Long.valueOf(String.valueOf(singleMap.get("targetFlow")));
            if (oldMap != null) {
                long sourceOldFlow = Long.valueOf(String.valueOf(oldMap.get("sourceFlow")));
                long targetOldFlow = Long.valueOf(String.valueOf(oldMap.get("targetFlow")));
                sourceFlow += sourceOldFlow;
                targetFlow += targetOldFlow;
                oldMap.put("sourceFlow", sourceFlow);
                oldMap.put("targetFlow", targetFlow);
            } else {
                oldMap = new HashMap<>( );
                oldMap.put("sourceFlow", sourceFlow);
                oldMap.put("targetFlow", targetFlow);
                oldMap.put("sourceType", sourceType);
                oldMap.put("targetType", targetType);
                oldMap.put("source", singleMap.get("source"));
                oldMap.put("target", singleMap.get("target"));
            }
            lineDataMap.put(keyType, oldMap);
        }
        List<Map<String, Object>> lineRet = lineDataMap.values( ).stream( ).collect(Collectors.toList( ));
        Map<String, Object> ret = new HashMap<>( );
        dataHeardList.sort((v1, v2) -> String.valueOf(v1.get("index")).compareTo(String.valueOf(v2.get("index"))));
        mindDataList.sort((v1, v2) -> String.valueOf(v1.get("index")).compareTo(String.valueOf(v2.get("index"))));
        ret.put("dataHeard", dataHeardList);
        ret.put("mindData", mindDataList);
        ret.put("FTPData", FTPData);
        ret.put("lineDatas", lineRet);
        LOGGER.error("FTP流向分析=====" + JSON.toJSONString(ret));
        return ret;
    }
}
